capt log close _all
log using   "${logs}FigureB1" , replace text
*------------

*==================================================================
* Figure B.1, B.2 and B.3
*==================================================================

use    "${dataout}MainDataset" , clear

keep if couple==1
keep if year>=2015 & year<=2017

// HH average values  
egen wies_fam_sum =rowtotal(wies1 wies2), missing
egen wealthFS =rowtotal(wealth1 wealth2), missing

// Label variables
label variable toll_fam_mean "Road toll (NOK)" 
label variable ptl_fam_km_mean "Bus lane (km)"
label variable wies_fam_sum "Total household income (1000 NOK)" 
label variable wealthFS "Total household wealth (1000 NOK)" 

*============================================================================
// Absorb 3 way FE from variables 
*============================================================================

local xvarlist  toll_fam_mean ptl_fam_mean  wies_fam_sum wealthFS  children antpers secondhome   age_fam_mean 

foreach x in `xvarlist' {
capt drop res_`x'
capt drop resM_`x'
reghdfe `x'  i.year , absorb($FE)  residuals(res_`x') 
sum `x'  if e(sample)==1
return list
dis "`r(mean)'"
gen resM_`x'=  res_`x' + `r(mean)'
}

/*============================================================================*/
 // BEV-TOLL/PTL: Absorb 3 way FE + HH and ind-work controls from variables  
 // Takes a long time to run
/*============================================================================*/

*=========== TOLL ==================
   
local yvarlist    toll_fam_mean 
local xvarlist    toll_fam_mean  BEV_fam_yes
foreach y in `yvarlist' {
foreach x in `xvarlist' {
capt drop res_`y'_`x'
capt drop resM_`y'_`x'
reghdfe `x'  ptl_fam_km_mean   $age    $distance  $time  $publictime  $publicquality   , ///
absorb($FE   $household  $income  $employment  $education  i.year  ) vce(cluster $clustervar)  residuals(res_`y'_`x') 
sum `x'  if e(sample)==1
return list
dis "`r(mean)'"
gen resM_`y'_`x'=  res_`y'_`x' + `r(mean)'

/*
preserve
keep `y' `x' resM_`y'_`x'  res_`y'_`x'
save "${temp}Binscatter_resM_`y'_`x'" , replace
restore
*/
}
}


*=========== PTL ==================

//  toll_fam_mean +  BEV_fam_yes   
capt gen ptl_fam_mean= ptl_fam_km_mean 

local yvarlist    ptl_fam_mean
local xvarlist    ptl_fam_mean  BEV_fam_yes

foreach y in `yvarlist' {
foreach x in `xvarlist' {
	
capt drop res_`y'_`x'
capt drop resM_`y'_`x'

reghdfe `x'  toll_fam_mean   $age    $distance  $time  $publictime  $publicquality   , ///
absorb($FE   $household  $income  $employment  $education  i.year  )  vce(cluster $clustervar) residuals(res_`y'_`x') 
sum `x'  if e(sample)==1
return list
dis "`r(mean)'"
gen resM_`y'_`x'=  res_`y'_`x' + `r(mean)'

/*
preserve
keep `y' `x' resM_`y'_`x'  res_`y'_`x'
save "${temp}Binscatter_resM_`y'_`x'" , replace
restore
*/
}
}

*============================================================================
// HH char: Absorb 3 way FE + HH and ind-work controls from variables  
// Takes a long time to run
*============================================================================

*=========== TOLL ==================

// wies_fam_sum   +  toll_fam_mean
local yvarlist    wies_fam_sum 
local xvarlist    wies_fam_sum toll_fam_mean
foreach y in `yvarlist' {
foreach x in `xvarlist' {
capt drop res_`y'_`x'
capt drop resM_`y'_`x'
reghdfe `x'  ptl_fam_km_mean   $age    $distance  $time  $publictime  $publicquality   , ///
absorb($FE   $household  /*$income*/ i.year#i.wealth1_decile  i.year#i.wealth2_decile  $employment  $education  i.year   ) residuals(res_`y'_`x') 
sum `x'  if e(sample)==1
return list
dis "`r(mean)'"
gen resM_`y'_`x'=  res_`y'_`x' + `r(mean)'
}
}

// wealthFS   +  toll_fam_mean
local yvarlist    wealthFS 
local xvarlist    wealthFS toll_fam_mean
foreach y in `yvarlist' {
foreach x in `xvarlist' {
capt drop res_`y'_`x'
capt drop resM_`y'_`x'
reghdfe `x'  ptl_fam_km_mean   $age    $distance  $time  $publictime  $publicquality   , ///
absorb($FE   $household  /*$income*/ i.year#i.wies1_decile i.year#i.wies2_decile  $employment  $education  i.year   ) residuals(res_`y'_`x') 
sum `x'  if e(sample)==1
return list
dis "`r(mean)'"
gen resM_`y'_`x'=  res_`y'_`x' + `r(mean)'
}
}

// antpers   +  toll_fam_mean
local yvarlist    antpers 
local xvarlist    antpers toll_fam_mean
foreach y in `yvarlist' {
foreach x in `xvarlist' {
capt drop res_`y'_`x'
capt drop resM_`y'_`x'
reghdfe `x'  ptl_fam_km_mean   $age    $distance  $time  $publictime  $publicquality   , ///
absorb($FE   /*$household*/  i.year#i.children  i.year#i.secondhome  $income  $employment  $education  i.year   ) residuals(res_`y'_`x') 
sum `x'  if e(sample)==1
return list
dis "`r(mean)'"
gen resM_`y'_`x'=  res_`y'_`x' + `r(mean)'
}
}

// age_fam_mean   +  toll_fam_mean
local yvarlist    age_fam_mean 
local xvarlist    age_fam_mean toll_fam_mean
foreach y in `yvarlist' {
foreach x in `xvarlist' {
capt drop res_`y'_`x'
capt drop resM_`y'_`x'
reghdfe `x'  ptl_fam_km_mean   /*$age*/    $distance  $time  $publictime  $publicquality   , ///
absorb($FE   $household  $income  $employment  $education  i.year   ) residuals(res_`y'_`x') 
sum `x'  if e(sample)==1
return list
dis "`r(mean)'"
gen resM_`y'_`x'=  res_`y'_`x' + `r(mean)'
}
}

// secondhome   +  toll_fam_mean
local yvarlist    secondhome 
local xvarlist  secondhome toll_fam_mean  
foreach y in `yvarlist' {
foreach x in `xvarlist' {
capt drop res_`y'_`x'
capt drop resM_`y'_`x'
reghdfe `x'  ptl_fam_km_mean   $age   $distance  $time  $publictime  $publicquality   , ///
absorb($FE   /*$household*/  i.year#i.children i.year#i.antpers_i_regstat_famnr   $income  $employment  $education  i.year   ) residuals(res_`y'_`x') 
sum `x'  if e(sample)==1
return list
dis "`r(mean)'"
gen resM_`y'_`x'=  res_`y'_`x' + `r(mean)'
}
}

// children   +  toll_fam_mean
local yvarlist    children 
local xvarlist  children toll_fam_mean  
foreach y in `yvarlist' {
foreach x in `xvarlist' {
capt drop res_`y'_`x'
capt drop resM_`y'_`x'
reghdfe `x'  ptl_fam_km_mean   $age   $distance  $time  $publictime  $publicquality   , ///
absorb($FE   /*$household*/  i.year#i.antpers_i_regstat_famnr i.year#i.secondhome  $income  $employment  $education  i.year   ) residuals(res_`y'_`x') 
sum `x'  if e(sample)==1
return list
dis "`r(mean)'"
gen resM_`y'_`x'=  res_`y'_`x' + `r(mean)'
}
}

*=========== PTL ==================

// wies_fam_sum   +  ptl_fam_mean
local yvarlist    wies_fam_sum 
local xvarlist    wies_fam_sum ptl_fam_mean
foreach y in `yvarlist' {
foreach x in `xvarlist' {
capt drop resP_`y'_`x'
capt drop resPM_`y'_`x'
reghdfe `x'  toll_fam_mean   $age    $distance  $time  $publictime  $publicquality   , ///
absorb($FE   $household  /*$income*/ i.year#i.wealth1_decile  i.year#i.wealth2_decile  $employment  $education  i.year   ) residuals(resP_`y'_`x') 
sum `x'  if e(sample)==1
return list
dis "`r(mean)'"
gen resPM_`y'_`x'=  resP_`y'_`x' + `r(mean)'
}
}

// wealthFS   +  ptl_fam_mean
local yvarlist    wealthFS 
local xvarlist    wealthFS ptl_fam_mean
foreach y in `yvarlist' {
foreach x in `xvarlist' {
capt drop resP_`y'_`x'
capt drop resPM_`y'_`x'
reghdfe `x'  toll_fam_mean   $age    $distance  $time  $publictime  $publicquality   , ///
absorb($FE   $household  /*$income*/ i.year#i.wies1_decile i.year#i.wies2_decile  $employment  $education  i.year   ) residuals(resP_`y'_`x') 
sum `x'  if e(sample)==1
return list
dis "`r(mean)'"
gen resPM_`y'_`x'=  resP_`y'_`x' + `r(mean)'
}
}

// antpers   +  ptl_fam_mean
local yvarlist    antpers 
local xvarlist    antpers ptl_fam_mean
foreach y in `yvarlist' {
foreach x in `xvarlist' {
capt drop resP_`y'_`x'
capt drop resPM_`y'_`x'
reghdfe `x'  toll_fam_mean   $age    $distance  $time  $publictime  $publicquality   , ///
absorb($FE   /*$household*/  i.year#i.children  i.year#i.secondhome  $income  $employment  $education  i.year   ) residuals(resP_`y'_`x') 
sum `x'  if e(sample)==1
return list
dis "`r(mean)'"
gen resPM_`y'_`x'=  resP_`y'_`x' + `r(mean)'
}
}

// age_fam_mean   +  ptl_fam_mean
local yvarlist    age_fam_mean 
local xvarlist    age_fam_mean ptl_fam_mean
foreach y in `yvarlist' {
foreach x in `xvarlist' {
capt drop resP_`y'_`x'
capt drop resPM_`y'_`x'
reghdfe `x'  toll_fam_mean   /*$age*/    $distance  $time  $publictime  $publicquality   , ///
absorb($FE   $household  $income  $employment  $education  i.year   ) residuals(resP_`y'_`x') 
sum `x'  if e(sample)==1
return list
dis "`r(mean)'"
gen resPM_`y'_`x'=  resP_`y'_`x' + `r(mean)'
}
}

// secondhome   +  ptl_fam_mean
local yvarlist   secondhome 
local xvarlist  secondhome ptl_fam_mean  
foreach y in `yvarlist' {
foreach x in `xvarlist' {
capt drop resP_`y'_`x'
capt drop resPM_`y'_`x'
reghdfe `x'  toll_fam_mean   $age   $distance  $time  $publictime  $publicquality   , ///
absorb($FE   /*$household*/  i.year#i.children i.year#i.antpers_i_regstat_famnr   $income  $employment  $education  i.year   ) residuals(resP_`y'_`x') 
sum `x'  if e(sample)==1
return list
dis "`r(mean)'"
gen resPM_`y'_`x'=  resP_`y'_`x' + `r(mean)'
}
}

// children   +  ptl_fam_mean
local yvarlist   children 
local xvarlist  children ptl_fam_mean  
foreach y in `yvarlist' {
foreach x in `xvarlist' {
capt drop resP_`y'_`x'
capt drop resPM_`y'_`x'
reghdfe `x'  toll_fam_mean   $age   $distance  $time  $publictime  $publicquality   , ///
absorb($FE    /*$household*/  i.year#i.antpers_i_regstat_famnr i.year#i.secondhome   $income  $employment  $education  i.year   ) residuals(resP_`y'_`x') 
sum `x'  if e(sample)==1
return list
dis "`r(mean)'"
gen resPM_`y'_`x'=  resP_`y'_`x' + `r(mean)'
}
}

/*
* Since the regressions above take a lot of time, store a temporary dataaset
* that includes the residualized values from above.
save "${datatemp}BinScatter_IdentifyingAssumptions_Couple" , replace

use "${datatemp}BinScatter_IdentifyingAssumptions_Couple", clear
*/

/***********  use BINSCATTER command in STATA ***********************/

*===============================================================================
// Binscatter of toll and PTL over income groups
*===============================================================================

*=== loop over the various x charactersitics
local yvarlist toll_fam_mean   		
local xvarlist wies_fam_sum wealthFS antpers secondhome age_fam_mean     
foreach y in `yvarlist' {
	foreach x in `xvarlist' {
		local laby: variable label `y'
		local labx: variable label `x'

		*=== No controls, lfit =================================================
		sum `x', det
		capt gen helpvar=1
		reghdfe `y'  `x'  if `x'>=`r(p1)' & `x'<=`r(p99)', ///
			absorb(helpvar)  vce(cluster $clustervar) 
		eststo
		drop helpvar 

		local coeff _b[`x']
		local fmtcoeff : dis %7.5f `coeff'
		dis `fmtcoeff'

		local se _se[`x']
		local fmtse : dis %7.6f `se'
		dis `fmtse'

		sum `x', det
		binscatter  `y'  `x'  if `x'>=`r(p1)' & `x'<=`r(p99)'  , ///
		line(lfit)   nquantiles(20) legend(off)  scale(1.6) lcolor(gs10)   ///
		ytitle("`laby'") ylab(0(5)15)   ///
		xtitle("`labx'")  ///
		graphregion(color(white))   yscale(titlegap(1.5)) xscale(titlegap(1.5)) ///
		subtitle("Coeff: `fmtcoeff'" "(`fmtse')") name(fig_`y'_`x'1, replace)
		
		* For saving the estimates as ster files with the right name 
		local savefig = "`x'"
		if "`savefig'" == "wies_fam_sum" {
			graph export     "${figures}FigureB1a.png" , replace
			graph export     "${figures}FigureB1a.pdf" , replace
			graph save       "${figures}FigureB1a.gph" , replace
		} 
		if "`savefig'" == "wealthFS" {
			graph export     "${figures}FigureB2a.png" , replace
			graph export     "${figures}FigureB2a.pdf" , replace
			graph save       "${figures}FigureB2a.gph" , replace
		}
		if "`savefig'" == "antpers" {
			graph export     "${figures}FigureB2d.png" , replace
			graph export     "${figures}FigureB2d.pdf" , replace
			graph save       "${figures}FigureB2d.gph" , replace
		}
		if "`savefig'" == "secondhome" {
			graph export     "${figures}FigureB2g.png" , replace
			graph export     "${figures}FigureB2g.pdf" , replace
			graph save       "${figures}FigureB2g.gph" , replace
		}
		if "`savefig'" == "age" {
			graph export     "${figures}FigureB2j.png" , replace
			graph export     "${figures}FigureB2j.pdf" , replace
			graph save       "${figures}FigureB2j.gph" , replace
		}

		*=== 3FE, lfit =========================================================
		sum `x', det
		capt gen helpvar=1
		reghdfe resM_`y'  resM_`x' if `x'>=`r(p1)' & `x'<=`r(p99)' , ///
			absorb(helpvar)  vce(cluster $clustervar) 
		eststo
		drop helpvar 

		local coeff _b[resM_`x']
		local fmtcoeff : dis %7.5f `coeff'
		dis `fmtcoeff'

		local se _se[resM_`x']
		local fmtse : dis %7.6f `se'
		dis `fmtse'

		sum `x', det
		binscatter  resM_`y'  resM_`x'   if `x'>=`r(p1)' & `x'<=`r(p99)' , ///
		linetype(lfit)    nquantiles(20)   legend(off) scale(1.6) lcolor(gs10)  reportreg  ///
		ytitle("`laby'")  ylab(0(5)15)   ///
		xtitle("`labx'")  ///
		graphregion(color(white))   yscale(titlegap(1.5)) xscale(titlegap(1.5)) ///
		subtitle("Coeff: `fmtcoeff'" "(`fmtse')") name(fig_`y'_`x'2, replace)
		if "`savefig'" == "wies_fam_sum" {
			graph export     "${figures}FigureB1b.png" , replace
			graph export     "${figures}FigureB1b.pdf" , replace
			graph save       "${figures}FigureB1b.gph" , replace
		} 
		if "`savefig'" == "wealthFS" {
			graph export     "${figures}FigureB2b.png" , replace
			graph export     "${figures}FigureB2b.pdf" , replace
			graph save       "${figures}FigureB2b.gph" , replace
		}
		if "`savefig'" == "antpers" {
			graph export     "${figures}FigureB2e.png" , replace
			graph export     "${figures}FigureB2e.pdf" , replace
			graph save       "${figures}FigureB2e.gph" , replace
		}
		if "`savefig'" == "secondhome" {
			graph export     "${figures}FigureB2h.png" , replace
			graph export     "${figures}FigureB2h.pdf" , replace
			graph save       "${figures}FigureB2h.gph" , replace
		}
		if "`savefig'" == "age" {
			graph export     "${figures}FigureB2k.png" , replace
			graph export     "${figures}FigureB2k.pdf" , replace
			graph save       "${figures}FigureB2k.gph" , replace
		}
	}
}

*===================================================================================== 
// Plot (residualized) correlations between TOLL and HH characteristics
// Absorb controls using regression! allows for same controls as main regression
*=====================================================================================

*=== loop over the various x charactersitics
local yvarlist    toll_fam_mean    
local xvarlist    wies_fam_sum wealthFS antpers secondhome age_fam_mean    
foreach y in `yvarlist' {
	foreach x in `xvarlist' {
		local laby: variable label `y'
		local labx: variable label `x'

		*=== HH and ind work controls - from detailed regression! , lfit =======
		sum `x', det
		capt gen helpvar=1
		reghdfe resM_`x'_`y'  resM_`x'_`x' if `x'>=`r(p1)' & `x'<=`r(p99)' ///
			, absorb(helpvar)  vce(cluster $clustervar) 
		eststo
		drop helpvar 

		local coeff _b[resM_`x'_`x']
		local fmtcoeff : dis %7.5f `coeff'
		dis `fmtcoeff'

		local se _se[resM_`x'_`x']
		local fmtse : dis %7.6f `se'
		dis `fmtse'

		sum `x', det
		binscatter  resM_`x'_`y'  resM_`x'_`x'   if `x'>=`r(p1)' & `x'<=`r(p99)' , ///
		linetype(lfit)  nquantiles(20)  legend(off) scale(1.6) lcolor(gs10)  reportreg  ///
		ytitle("`laby'") ylab(0(5)15)  ///
		xtitle("`labx'")  ///
		graphregion(color(white))   yscale(titlegap(1.5)) xscale(titlegap(1.5)) ///
		subtitle("Coeff: `fmtcoeff'" "(`fmtse')")  name(fig_`y'_`x'3, replace)
		local savefig = "`x'"
		if "`savefig'" == "wies_fam_sum" {
			graph export     "${figures}FigureB1c.png" , replace
			graph export     "${figures}FigureB1c.pdf" , replace
			graph save       "${figures}FigureB1c.gph" , replace
		} 
		if "`savefig'" == "wealthFS" {
			graph export     "${figures}FigureB2c.png" , replace
			graph export     "${figures}FigureB2c.pdf" , replace
			graph save       "${figures}FigureB2c.gph" , replace
		}
		if "`savefig'" == "antpers" {
			graph export     "${figures}FigureB2f.png" , replace
			graph export     "${figures}FigureB2f.pdf" , replace
			graph save       "${figures}FigureB2f.gph" , replace
		}
		if "`savefig'" == "secondhome" {
			graph export     "${figures}FigureB2i.png" , replace
			graph export     "${figures}FigureB2i.pdf" , replace
			graph save       "${figures}FigureB2i.gph" , replace
		}
		if "`savefig'" == "age" {
			graph export     "${figures}FigureB2l.png" , replace
			graph export     "${figures}FigureB2l.pdf" , replace
			graph save       "${figures}FigureB2l.gph" , replace
		}
	}
}

*===============================================================================
// Plot (residualized) correlations between PTL and HH characteristics 
*===============================================================================

*=== loop over the various x charactersitics
local yvarlist ptl_fam_km_mean   		
local xvarlist wies_fam_sum wealthFS antpers secondhome age_fam_mean     
foreach y in `yvarlist' {
	foreach x in `xvarlist' {
		local laby: variable label `y'
		local labx: variable label `x'

		*=== No controls, lfit
		sum `x' , det
		capt gen helpvar=1
		reghdfe `y'  `x'  if `x'>=`r(p1)' & `x'<=`r(p99)' ///
			, absorb(helpvar)  vce(cluster $clustervar) 
		eststo
		drop helpvar 

		local coeff _b[`x']
		local fmtcoeff : dis %7.5f `coeff'
		dis `fmtcoeff'

		local se _se[`x']
		local fmtse : dis %7.6f `se'
		dis `fmtse'

		sum `x', det
		binscatter  `y'  `x'  if `x'>=`r(p1)' & `x'<=`r(p99)'  , ///
		line(lfit)   nquantiles(20) legend(off)  scale(1.6) lcolor(gs10)   ///
		ytitle("`laby'") ylab(0(0.2)1)   ///
		xtitle("`labx'")  ///
		graphregion(color(white))   yscale(titlegap(1.5)) xscale(titlegap(1.5)) ///
		subtitle("Coeff: `fmtcoeff'" "(`fmtse')") name(fig_`y'_`x'1, replace)
		local savefig = "`x'"
		if "`savefig'" == "wies_fam_sum" {
			graph export     "${figures}FigureB1d.png" , replace
			graph export     "${figures}FigureB1d.pdf" , replace
			graph save       "${figures}FigureB1d.gph" , replace
		} 
		if "`savefig'" == "wealthFS" {
			graph export     "${figures}FigureB3a.png" , replace
			graph export     "${figures}FigureB3a.pdf" , replace
			graph save       "${figures}FigureB3a.gph" , replace
		}
		if "`savefig'" == "antpers" {
			graph export     "${figures}FigureB3d.png" , replace
			graph export     "${figures}FigureB3d.pdf" , replace
			graph save       "${figures}FigureB3d.gph" , replace
		}
		if "`savefig'" == "secondhome" {
			graph export     "${figures}FigureB3g.png" , replace
			graph export     "${figures}FigureB3g.pdf" , replace
			graph save       "${figures}FigureB3g.gph" , replace
		}
		if "`savefig'" == "age" {
			graph export     "${figures}FigureB3j.png" , replace
			graph export     "${figures}FigureB3j.pdf" , replace
			graph save       "${figures}FigureB3j.gph" , replace
		}

		*=== 3FE, lfit
		sum `x', det
		capt gen helpvar=1
		reghdfe resM_`y'  resM_`x' if `x'>=`r(p1)' & `x'<=`r(p99)', ///
			absorb(helpvar)  vce(cluster $clustervar) 
		eststo
		drop helpvar 

		local coeff _b[resM_`x']
		local fmtcoeff : dis %7.5f `coeff'
		dis `fmtcoeff'

		local se _se[resM_`x']
		local fmtse : dis %7.6f `se'
		dis `fmtse'

		sum `x', det
		binscatter  resM_`y'  resM_`x'   if `x'>=`r(p1)' & `x'<=`r(p99)' , ///
		linetype(lfit)    nquantiles(20)   legend(off) scale(1.6) lcolor(gs10)  reportreg  ///
		ytitle("`laby'") ylab(0(0.2)1)   ///
		xtitle("`labx'")  ///
		graphregion(color(white))   yscale(titlegap(1.5)) xscale(titlegap(1.5)) ///
		subtitle("Coeff: `fmtcoeff'" "(`fmtse')") name(fig_`y'_`x'2, replace)
		if "`savefig'" == "wies_fam_sum" {
			graph export     "${figures}FigureB1e.png" , replace
			graph export     "${figures}FigureB1e.pdf" , replace
			graph save       "${figures}FigureB1e.gph" , replace
		} 
		if "`savefig'" == "wealthFS" {
			graph export     "${figures}FigureB3b.png" , replace
			graph export     "${figures}FigureB3b.pdf" , replace
			graph save       "${figures}FigureB3b.gph" , replace
		}
		if "`savefig'" == "antpers" {
			graph export     "${figures}FigureB3e.png" , replace
			graph export     "${figures}FigureB3e.pdf" , replace
			graph save       "${figures}FigureB3e.gph" , replace
		}
		if "`savefig'" == "secondhome" {
			graph export     "${figures}FigureB3h.png" , replace
			graph export     "${figures}FigureB3h.pdf" , replace
			graph save       "${figures}FigureB3h.gph" , replace
		}
		if "`savefig'" == "age" {
			graph export     "${figures}FigureB3k.png" , replace
			graph export     "${figures}FigureB3k.pdf" , replace
			graph save       "${figures}FigureB3k.gph" , replace
		}
	}
}

*===============================================================================
// Plot (residualized) correlations between PTL and HH characteristics
// Absorb controls using regression! allows for same controls as main regression
*===============================================================================

*=== loop over the various x charactersitics
local yvarlist    ptl_fam_km_mean    
local xvarlist    wies_fam_sum wealthFS antpers secondhome age_fam_mean             
foreach y in `yvarlist' {
	foreach x in `xvarlist' {

		local laby: variable label `y'
		local labx: variable label `x'

		* === HH and ind work controls - from detailed regression! , lfit
		sum `x', det
		capt gen helpvar=1
		reghdfe resPM_`x'_`y'  resPM_`x'_`x' if `x'>=`r(p1)' & `x'<=`r(p99)', ///
			absorb(helpvar)  vce(cluster $clustervar) 
		eststo
		drop helpvar 

		local coeff _b[resPM_`x'_`x']
		local fmtcoeff : dis %7.5f `coeff'
		dis `fmtcoeff'

		local se _se[resPM_`x'_`x']
		local fmtse : dis %7.6f `se'
		dis `fmtse'

		sum `x', det
		binscatter  resPM_`x'_`y'  resPM_`x'_`x'   if `x'>=`r(p1)' & `x'<=`r(p99)' , ///
			linetype(lfit)  nquantiles(20)  legend(off) scale(1.6) lcolor(gs10)  reportreg  ///
			ytitle("`laby'") ylab(0(0.2)1)   ///
			xtitle("`labx'")  ///
			graphregion(color(white))   yscale(titlegap(1.5)) xscale(titlegap(1.5)) ///
			subtitle("Coeff: `fmtcoeff'" "(`fmtse')") name(fig_`y'_`x'3, replace)
		local savefig = "`x'"
		if "`savefig'" == "wies_fam_sum" {
			graph export     "${figures}FigureB1f.png" , replace
			graph export     "${figures}FigureB1f.pdf" , replace
			graph save       "${figures}FigureB1f.gph" , replace
		} 
		if "`savefig'" == "wealthFS" {
			graph export     "${figures}FigureB3c.png" , replace
			graph export     "${figures}FigureB3c.pdf" , replace
			graph save       "${figures}FigureB3c.gph" , replace
		}
		if "`savefig'" == "antpers" {
			graph export     "${figures}FigureB3f.png" , replace
			graph export     "${figures}FigureB3f.pdf" , replace
			graph save       "${figures}FigureB3f.gph" , replace
		}
		if "`savefig'" == "secondhome" {
			graph export     "${figures}FigureB3i.png" , replace
			graph export     "${figures}FigureB3i.pdf" , replace
			graph save       "${figures}FigureB3i.gph" , replace
		}
		if "`savefig'" == "age" {
			graph export     "${figures}FigureB3l.png" , replace
			graph export     "${figures}FigureB3l.pdf" , replace
			graph save       "${figures}FigureB3l.gph" , replace
		}
	}
}

*-------------------------------------------------------------------------------
log close